CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
CMAKE_POLICY(VERSION 2.8)

IF(NOT CUDA_FOUND)
  FIND_PACKAGE(CUDA 7.0 REQUIRED)
ENDIF()

ADD_CUSTOM_COMMAND(
   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
   OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lib/libnccl.so
   COMMAND env CUDA_HOME=${CUDA_TOOLKIT_ROOT_DIR} NVCC=${CUDA_NVCC_EXECUTABLE} BUILDDIR=${CMAKE_CURRENT_BINARY_DIR} make -j `getconf _NPROCESSORS_ONLN`
)

ADD_CUSTOM_TARGET(nccl ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/lib/libnccl.so)

INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/include/nccl.h DESTINATION "include")
